package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.RuleRidePhotoLog;
import com.tbit.uqbike.object.pojo.vo.RuleRideLogVo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.RuleRideLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 非文明骑行记录
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 16:35 2022/7/15
 */
@RestController
@RequestMapping("/ruleRideLog")
@Slf4j
public class RuleRideLogController {

    @Autowired
    private RuleRideLogService ruleRideLogService;

    /**
     * 分账查询非文明骑行记录
     *
     * @param phone     手机号码
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param state     状态
     * @param pageNO    分页页码数
     * @param rowCount  每页大小
     * @param mapType   地图类型
     * @return
     */
    @GetMapping("/queryPage")
    public Result<PageResult<RuleRideLogVo>> queryPage(String phone, String startTime, String endTime, Integer state,Integer accountId,
                                                       @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount,
                                                       @RequestParam(defaultValue = "2") Integer mapType) {
        PageResult<RuleRideLogVo> ruleRideLogs = ruleRideLogService.queryPage(phone, startTime, endTime, state, pageNO, rowCount, mapType,accountId);
        //Integer total = ruleRideLogService.queryCount(phone, startTime, endTime, state);
        return Result.success(ruleRideLogs);
    }

    /**
     * 非文明骑行记录
     * @param phone     手机号码
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param state     状态
     * @param mapType   地图类型
     * @return
     */
    @GetMapping("/exportRuleRideLog")
    public Result<Object> exportRuleRideLog(String phone, String startTime, String endTime, Integer state,Integer accountId, @RequestParam(defaultValue = "2") Integer mapType) {
        try {
            List<RuleRideLogVo> list = ruleRideLogService.queryPage(phone, startTime, endTime, state, 1, 65535, mapType, accountId).getList();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String fileName = String.format("%s~%s 非文明骑行记录", sdf.format(sdf.parse(startTime)), sdf.format(sdf.parse(endTime)));
            ExcelUtil<RuleRideLogVo> excelUtil = new ExcelUtil<>(RuleRideLogVo.class);
            JsonResult result = excelUtil.exportExcel(list, fileName);
            return Result.success(result.getData());
        }catch (Exception e){
            log.error("非文明骑行记录导出异常", e);
        }
        return Result.error("导出失败，请联系管理员");
    }



    /**
     * 处理申诉
     *
     * @param rspRet 处理结果
     * @param rspMsg 回复消息
     * @param rideId 非文明骑行主键id
     * @return
     */
    @PostMapping("/handel")
    public Result handel(Integer rspRet, String rspMsg, Integer rideId, @RequestParam(defaultValue = "0") Integer sendSms) {
        Assert.notNull(rideId, "account_id_not_valid");
        Assert.notNull(rspRet, "reson_not_null");
        ruleRideLogService.edit(rspRet, rspMsg, rideId, sendSms);
        return Result.success();
    }

    /**
     * 获取图片信息
     *
     * @param rideId 非文明骑行主键id
     * @return
     */
    @GetMapping("/getPhoto")
    public Result getPhoto(Integer rideId) {
        Assert.notNull(rideId, "account_id_not_valid");
        List<RuleRidePhotoLog> ruleRidePhotoLogs = ruleRideLogService.getPhoto(rideId);
        return Result.success(ruleRidePhotoLogs);
    }

    /**
     * 通过ftp url获取图片信息
     *
     * @param ftpUrl ftpUrl
     * @return
     */
    @PostMapping("/getByFtpUrl")
    public Result getByFtpUrl(String ftpUrl) {
        Assert.notNull(ftpUrl, "account_id_not_valid");
        RuleRidePhotoLog ruleRidePhotoLogs = ruleRideLogService.getByFtpUrl(ftpUrl);
        return Result.success(ruleRidePhotoLogs);
    }
}
